package cn.michael.dev.data.structure.stack;


/**
 * Created by hufenggang on 2021/8/24.
 *
 * 基于数组实现栈
 */
public class ArrayStack {
    // 数组
    private String[] items;
    // 栈中元素个数
    private int count;
    // 栈大小
    private int size;

    public ArrayStack(int size) {
        this.items = new String[size];
        this.size = size;
        this.count = 0;
    }

    /**
     * 入栈操作
     *
     * @param item
     * @return
     */
    public boolean push(String item) {
        // 数组空间不够，直接返回false
        if (count == size) {
            return false;
        }

        items[count] = item;
        count++;
        return true;
    }

    /**
     * 出栈操作
     *
     * @return
     */
    public String pop() {
        // 栈为空，直接返回false
        if (count == 0) {
            return null;
        }

        String tmp = items[count - 1];
        count--;
        return tmp;
    }
}
